package com.example.watertest;
import java.io.IOException;
import java.util.Calendar;
 
import org.andengine.audio.music.Music;
import org.andengine.audio.music.MusicFactory;
import org.andengine.engine.camera.BoundCamera;
import org.andengine.engine.camera.Camera;
import org.andengine.engine.camera.SmoothCamera;
import org.andengine.engine.camera.ZoomCamera;
import org.andengine.engine.options.EngineOptions;
import org.andengine.engine.options.ScreenOrientation;
import org.andengine.engine.options.WakeLockOptions;
import org.andengine.engine.options.resolutionpolicy.FillResolutionPolicy;
import org.andengine.entity.particle.BatchedSpriteParticleSystem;
import org.andengine.entity.particle.ParticleSystem;
import org.andengine.entity.particle.emitter.CircleOutlineParticleEmitter;
import org.andengine.entity.particle.emitter.PointParticleEmitter;
import org.andengine.entity.particle.emitter.RectangleOutlineParticleEmitter;
import org.andengine.entity.particle.initializer.AccelerationParticleInitializer;
import org.andengine.entity.particle.initializer.AlphaParticleInitializer;
import org.andengine.entity.particle.initializer.BlendFunctionParticleInitializer;
import org.andengine.entity.particle.initializer.ExpireParticleInitializer;
import org.andengine.entity.particle.initializer.GravityParticleInitializer;
import org.andengine.entity.particle.initializer.RotationParticleInitializer;
import org.andengine.entity.particle.modifier.ScaleParticleModifier;
import org.andengine.entity.primitive.Rectangle;
import org.andengine.entity.scene.IOnSceneTouchListener;
import org.andengine.entity.scene.Scene;
import org.andengine.entity.scene.background.AutoParallaxBackground;
import org.andengine.entity.scene.background.Background;
import org.andengine.entity.scene.background.EntityBackground;
import org.andengine.entity.scene.background.ParallaxBackground;
import org.andengine.entity.scene.background.ParallaxBackground.ParallaxEntity;
import org.andengine.entity.scene.background.RepeatingSpriteBackground;
import org.andengine.entity.scene.background.SpriteBackground;
import org.andengine.entity.sprite.AnimatedSprite;
import org.andengine.entity.sprite.AnimatedSprite.IAnimationListener;
import org.andengine.entity.sprite.ButtonSprite;
import org.andengine.entity.sprite.Sprite;
import org.andengine.entity.sprite.TiledSprite;
import org.andengine.entity.sprite.UncoloredSprite;
import org.andengine.entity.text.Text;
import org.andengine.opengl.font.Font;
import org.andengine.opengl.font.FontFactory;
import org.andengine.opengl.texture.ITexture;
import org.andengine.opengl.texture.TextureOptions;
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlas;
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlasTextureRegionFactory;
import org.andengine.opengl.texture.atlas.bitmap.BuildableBitmapTextureAtlas;
import org.andengine.opengl.texture.atlas.bitmap.source.IBitmapTextureAtlasSource;
import org.andengine.opengl.texture.atlas.buildable.BuildableTextureAtlas;
import org.andengine.opengl.texture.atlas.buildable.builder.BlackPawnTextureAtlasBuilder;
import org.andengine.opengl.texture.atlas.buildable.builder.ITextureAtlasBuilder.TextureAtlasBuilderException;
import org.andengine.opengl.texture.bitmap.AssetBitmapTexture;
import org.andengine.opengl.texture.bitmap.BitmapTextureFormat;
import org.andengine.opengl.texture.region.ITextureRegion;
import org.andengine.opengl.texture.region.ITiledTextureRegion;
import org.andengine.opengl.texture.region.TextureRegionFactory;
import org.andengine.ui.activity.SimpleBaseGameActivity;
import org.andengine.util.math.MathUtils;
import org.andengine.util.modifier.IModifier;
import org.andengine.entity.Entity;
import org.andengine.entity.IEntity;
import org.andengine.entity.modifier.CardinalSplineMoveModifier;
import org.andengine.entity.modifier.CardinalSplineMoveModifier.CardinalSplineMoveModifierConfig;
import org.andengine.entity.modifier.ColorModifier;
import org.andengine.entity.modifier.DelayModifier;
import org.andengine.entity.modifier.IEntityModifier;
import org.andengine.entity.modifier.IEntityModifier.IEntityModifierListener;
import org.andengine.entity.modifier.IEntityModifier.IEntityModifierMatcher;
import org.andengine.entity.modifier.JumpModifier;
import org.andengine.entity.modifier.LoopEntityModifier;
import org.andengine.entity.modifier.MoveByModifier;
import org.andengine.entity.modifier.MoveModifier;
import org.andengine.entity.modifier.MoveXModifier;
import org.andengine.entity.modifier.ParallelEntityModifier;
import org.andengine.entity.modifier.PathModifier;
import org.andengine.entity.modifier.PathModifier.Path;
import org.andengine.entity.modifier.RotationModifier;
import org.andengine.entity.modifier.ScaleModifier;
import org.andengine.entity.modifier.SequenceEntityModifier;
import org.andengine.input.touch.TouchEvent;

import android.graphics.Color;
import android.opengl.GLES20;
import android.util.Log;
import android.widget.Toast;
 
 
 
public class MainActivityCamera extends SimpleBaseGameActivity implements IOnSceneTouchListener {
 
protected static final IAnimationListener AnimatedSprite = null;
private Camera mCamera;
private static int WIDTH = 1600;
private static int HEIGHT= 480;
private Font miFuente;
private BitmapTextureAtlas miAtlas;
private BitmapTextureAtlas AtlasFondo;
private ITextureRegion texturaBackGround;
private Sprite charSprite, Boton, backleft, backright;
private ITiledTextureRegion texturaAnimada, texturaSonido;
private AnimatedSprite spriteAnimado;
private Text miTexto;
private int cuenta = 0;
private Music music;

@Override
public EngineOptions onCreateEngineOptions() {
 
// Definimos nuestra camara
	//ZoomCamera mCamera = new ZoomCamera(0, 0, WIDTH, HEIGHT);	// WIDTH = 800;
	// Divide the camera width/height by 1.5x (Zoom in)
	//mCamera.setZoomFactor(1.5f);
	// Camera movement speeds
	final float maxVelocityX = 10;
	final float maxVelocityY = 5;
	// Camera zoom speed
	final float maxZoomFactorChange = 5;
	    
	// Create smooth camera
	mCamera = new SmoothCamera(0, 0, WIDTH, HEIGHT, maxVelocityX, maxVelocityY, maxZoomFactorChange);
	    

EngineOptions engineOptions = new EngineOptions(true, ScreenOrientation.LANDSCAPE_FIXED, new FillResolutionPolicy(), mCamera);
//EngineOptions(es full screen?, Cual es la orientacion de la pantalla?, Como actuaremos ante distintas resoluciones?, y la camara)
// impedimos que la pantalla se apague por inactividad
engineOptions.setWakeLockOptions(WakeLockOptions.SCREEN_ON);
// Return the engineOptions object, passing it to the engine
//para activar la musica
engineOptions.getAudioOptions().setNeedsMusic(true);
return engineOptions;
}
 
@Override
protected void onCreateResources() throws IOException {
 
//primero debemos indicar donde estan las imagenes
BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/");
//luego crear el atlas, darle medidas y un tipo de textura.
miAtlas = new BitmapTextureAtlas(getTextureManager(), 800, 800, TextureOptions.BILINEAR);
AtlasFondo = new BitmapTextureAtlas(getTextureManager(), 800, 800, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
texturaAnimada = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(miAtlas, this, "boton_menu.jpg", 70, 0, 2, 1);
texturaSonido = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(miAtlas, this, "sonido.jpg", 10, 100, 2, 1);
texturaBackGround = BitmapTextureAtlasTextureRegionFactory.createFromAsset(AtlasFondo, this, "back.jpg", 0, 100);

//y la cargamos
miAtlas.load();
AtlasFondo.load();


try
{
    music = MusicFactory.createMusicFromAsset(mEngine.getMusicManager(), this,"musica/musica.mp3");
    music.setLooping(true);
}
catch (IOException e)
{
    e.printStackTrace();
}



}


 
@Override
protected Scene onCreateScene() {
 
Scene sceneEjemplo = new Scene();


final int halfTextureWidth = (int) (texturaBackGround.getWidth() * 0.5f);
final int halfTextureHeight = (int) (texturaBackGround.getHeight() * 0.5f);

// Create left background sprite
backleft = new Sprite(halfTextureWidth, halfTextureHeight, texturaBackGround,
    mEngine.getVertexBufferObjectManager())
;
// Attach left background sprite to the background scene
sceneEjemplo.attachChild(backleft);

//Create the right background sprite, positioned directly to the right of the first segment
backright = new Sprite(backleft.getX() + backleft.getWidth(),
 halfTextureHeight, texturaBackGround,
 mEngine.getVertexBufferObjectManager());

//Attach right background sprite to the background scene
sceneEjemplo.attachChild(backright);




/* Create the buttonSprite object in the center of the Scene */
ButtonSprite buttonSprite = new ButtonSprite(WIDTH * 0.5f, HEIGHT * 0.5f, texturaAnimada, mEngine.getVertexBufferObjectManager()) {
  /* Override the onAreaTouched() event method */
  @Override
  public boolean onAreaTouched(TouchEvent pSceneTouchEvent,
      float pTouchAreaLocalX, float pTouchAreaLocalY) {
    /* If buttonSprite is touched with the finger */
    if(pSceneTouchEvent.isActionDown()){
      /* When the button is pressed, we can create an event 
       * In this case, we're simply displaying a quick toast */
    	toastOnUiThread("Clicked", Toast.LENGTH_SHORT);
    }
    /* In order to allow the ButtonSprite to swap tiled texture region 
     * index on our buttonSprite object, we must return the super method */
    return super.onAreaTouched(pSceneTouchEvent, pTouchAreaLocalX, pTouchAreaLocalY);
  }
};

/* Register the buttonSprite as a 'touchable' Entity */
sceneEjemplo.registerTouchArea(buttonSprite);
/* Attach the buttonSprite to the Scene */
sceneEjemplo.attachChild(buttonSprite);

/* Create the music mute/unmute button */
ButtonSprite mMuteButton = new ButtonSprite(WIDTH * 0.7f, HEIGHT * 0.7f, texturaSonido, getVertexBufferObjectManager()) {

  /* Override the onAreaTouched() method allowing us to define custom
  * actions */
	@Override
	 public boolean onAreaTouched(TouchEvent pSceneTouchEvent,
		      float pTouchAreaLocalX, float pTouchAreaLocalY) {
		    /* In the event the mute button is pressed down on... */
		    if (pSceneTouchEvent.isActionDown()) {
		      if (music.isPlaying()) {
		        /*  If music is playing, pause it and set tile index to  MUTE  */
		        this.setCurrentTileIndex(1);
		        music.pause();
		      } else {
		        /* If music is paused, play it and set tile index to UNMUTE */
		    	  
		    	  this.setCurrentTileIndex(0);
		    	  music.play();
		        
		      }
		      return true;
		    }
		    return super.onAreaTouched(pSceneTouchEvent, pTouchAreaLocalX,
		        pTouchAreaLocalY);
		  }
	

};

music.setLooping(true);
music.play();
/* Set the current tile index to unmuted on application startup */

/* Register and attach the mMuteButton to the Scene */
sceneEjemplo.registerTouchArea(mMuteButton);
sceneEjemplo.attachChild(mMuteButton);












return sceneEjemplo;


}

@Override
public synchronized void onResumeGame() {
  super.onResumeGame();
  
	  music.play();
  
}


@Override
public synchronized void onPauseGame() {
  super.onPauseGame();
  
  /* Always pause the music on pause */
  if(music != null && music.isPlaying()){
	  music.pause();
  }
}

@Override
public boolean onSceneTouchEvent(Scene pScene, TouchEvent pSceneTouchEvent) {
 
  
  return false;
}
 
}